package com.gisocn.onemap.mvp.presenter;

import android.text.TextUtils;
import com.esri.android.map.Layer;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.android.map.ags.ArcGISLayerInfo;
import com.esri.core.map.CallbackListener;
import com.esri.core.map.FeatureResult;
import com.esri.core.map.Field;
import com.esri.core.tasks.SpatialRelationship;
import com.esri.core.tasks.query.QueryParameters;
import com.esri.core.tasks.query.QueryTask;
import com.gisocn.onemap.base.Constant;
import com.gisocn.onemap.model.FieldDataBean;
import com.gisocn.onemap.model.QueryXML;
import com.gisocn.onemap.mvp.MvpBasePresenter;
import com.gisocn.onemap.mvp.contact.ColumnSelectedContact;
import com.gisocn.onemap.utils.PullHelper;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.xmlpull.v1.XmlPullParserException;

/* loaded from: classes2.dex */
public class ColumnSelectedPresenter extends MvpBasePresenter<ColumnSelectedContact.View> implements ColumnSelectedContact.Presenter {
    private final List<FieldDataBean> resultDataList = new ArrayList();
    private final List<ArcGISLayerInfo> layerInfoList = new ArrayList();

    @Override // com.gisocn.onemap.mvp.contact.ColumnSelectedContact.Presenter
    public void getHttpColumnData(Layer layer, String str) {
        ArcGISLayerInfo arcGISLayerInfo;
        if (this.resultDataList.size() > 0) {
            this.resultDataList.clear();
        }
        if (layer != null && (layer instanceof ArcGISDynamicMapServiceLayer)) {
            ArcGISDynamicMapServiceLayer arcGISDynamicMapServiceLayer = (ArcGISDynamicMapServiceLayer) layer;
            arcGISDynamicMapServiceLayer.retrieveLegendInfo();
            ArcGISLayerInfo[] layers = arcGISDynamicMapServiceLayer.getLayers();
            if (layers != null) {
                if (this.layerInfoList.size() > 0) {
                    this.layerInfoList.clear();
                }
                getLayerInfo(layers, str);
            }
        }
        if (this.layerInfoList.size() <= 0 || (arcGISLayerInfo = this.layerInfoList.get(0)) == null) {
            return;
        }
        getQueryData(layer.getQueryUrl(arcGISLayerInfo.getId()));
    }

    @Override // com.gisocn.onemap.mvp.contact.ColumnSelectedContact.Presenter
    public void getXmlColumnData(String str) {
        FileInputStream fileInputStream;
        ArrayList<QueryXML.Field> fields;
        if (this.resultDataList.size() > 0) {
            this.resultDataList.clear();
        }
        if (!TextUtils.isEmpty(str)) {
            File file = new File(Constant.SAVE_PATH_SHP + str + ".xml");
            if (file.exists()) {
                try {
                    fileInputStream = new FileInputStream(file);
                    try {
                        QueryXML queryXML = PullHelper.getQueryXML(fileInputStream);
                        if (queryXML != null && (fields = queryXML.getFields()) != null && fields.size() > 0) {
                            Iterator<QueryXML.Field> it = fields.iterator();
                            while (it.hasNext()) {
                                QueryXML.Field next = it.next();
                                this.resultDataList.add(new FieldDataBean(next.getName(), next.getAliasname()));
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        try {
                            fileInputStream.close();
                        } catch (IOException e2) {
                            e = e2;
                            e.printStackTrace();
                            getView().columnDataSuccess(this.resultDataList);
                        }
                    } catch (XmlPullParserException e3) {
                        e3.printStackTrace();
                        try {
                            fileInputStream.close();
                        } catch (IOException e4) {
                            e = e4;
                            e.printStackTrace();
                            getView().columnDataSuccess(this.resultDataList);
                        }
                    }
                } catch (FileNotFoundException e5) {
                    e5.printStackTrace();
                }
                try {
                    fileInputStream.close();
                } catch (IOException e6) {
                    e = e6;
                    e.printStackTrace();
                    getView().columnDataSuccess(this.resultDataList);
                }
            }
        }
        getView().columnDataSuccess(this.resultDataList);
    }

    private void getQueryData(String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        QueryParameters queryParameters = new QueryParameters();
        queryParameters.setWhere("1=1");
        queryParameters.setOutFields(new String[]{"*"});
        queryParameters.setSpatialRelationship(SpatialRelationship.INTERSECTS);
        new QueryTask(str).execute(queryParameters, new CallbackListener<FeatureResult>() { // from class: com.gisocn.onemap.mvp.presenter.ColumnSelectedPresenter.1
            @Override // com.esri.core.map.CallbackListener
            public void onError(Throwable th) {
            }

            @Override // com.esri.core.map.CallbackListener
            public void onCallback(FeatureResult featureResult) {
                List<Field> fields;
                if (featureResult != null && (fields = featureResult.getFields()) != null && fields.size() > 0) {
                    for (Field field : fields) {
                        ColumnSelectedPresenter.this.resultDataList.add(new FieldDataBean(field.getName(), field.getAlias()));
                    }
                }
                ColumnSelectedPresenter.this.getView().columnDataSuccess(ColumnSelectedPresenter.this.resultDataList);
            }
        });
    }

    private void getLayerInfo(ArcGISLayerInfo[] arcGISLayerInfoArr, String str) {
        for (ArcGISLayerInfo arcGISLayerInfo : arcGISLayerInfoArr) {
            ArcGISLayerInfo[] layers = arcGISLayerInfo.getLayers();
            if (layers != null && layers.length > 0) {
                getLayerInfo(layers, str);
            } else if (arcGISLayerInfo.isVisible() && str.contains(String.valueOf(arcGISLayerInfo.getId()))) {
                this.layerInfoList.add(arcGISLayerInfo);
            }
        }
    }
}
